package com.bcts.rrh.drawingboard.shapeview;

import android.graphics.Color;
import android.opengl.Matrix;
import android.util.Log;

import com.bcts.rrh.drawingboard.utils.VaryTools;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

import static android.content.ContentValues.TAG;
import static com.bcts.rrh.drawingboard.Constants.*;


public class Cube extends Shape_3d {
    private FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
    private FloatBuffer mTextureBuffer;//纹理坐标数据缓冲
    float mAngleY;//绕Y轴旋转
    float mAngleZ;//绕Z轴旋转
    float scale;//立方体高度
    float scalezoom;//缩放尺寸
    int vCount;//顶点数量
    int textureId;//纹理ID
    int color;
    float[] tempVerteices;
    ArrayList<Float> inittempVerteices;//初始的坐标点数组
    float minX;//x轴最小位置
    float maxX;//x轴最大位置
    float minY;//y轴最小位置
    float maxY;//y轴最大位置
    float minZ;//z轴最小位置
    float maxZ;//z轴最大位置
    float midX;//中心点坐标
    float midY;
    float midZ;
    public float xOffset;//在轴上移动的位置
    public float yOffset;
    public float zOffset;

    public Cube(int textureId,int color,float scale,float length,float width,float xOffset,float yOffset,float zOffset)
    {
        this.scale=scale;
        this.scalezoom=1.0f;
        this.textureId=textureId;
        this.color=color;
        this.xOffset=xOffset;
        this.yOffset=yOffset;
        this.zOffset=zOffset;
        //113.34823 mAngleZ=24.183098
        //初始化角度让看得更像立方体
        mAngleY=85.20128f;
        mAngleZ=7.9459743f;
        vCount=36;
        float UNIT_SIZE=0.5f;//单位长度
        float UNIT_HIGHT=0.5f;
        float[] verteices=
                {
                        //顶面,5,6
                        UNIT_SIZE*length,UNIT_HIGHT*scale,-UNIT_SIZE*width,
                        -UNIT_SIZE*length,UNIT_HIGHT*scale,-UNIT_SIZE*width,
                        UNIT_SIZE*length,UNIT_HIGHT*scale,UNIT_SIZE*width,

                        UNIT_SIZE*length,UNIT_HIGHT*scale,UNIT_SIZE*width,
                        -UNIT_SIZE*length,UNIT_HIGHT*scale,UNIT_SIZE*width,
                        -UNIT_SIZE*length,UNIT_HIGHT*scale,-UNIT_SIZE*width,

                        //后面7,8
                        UNIT_SIZE*length,UNIT_HIGHT*scale,-UNIT_SIZE*width,
                        UNIT_SIZE*length,-UNIT_HIGHT*scale,-UNIT_SIZE*width,
                        -UNIT_SIZE*length,UNIT_HIGHT*scale,-UNIT_SIZE*width,

                        -UNIT_SIZE*length,UNIT_HIGHT*scale,-UNIT_SIZE*width,
                        UNIT_SIZE*length,-UNIT_HIGHT*scale,-UNIT_SIZE*width,
                        -UNIT_SIZE*length,-UNIT_HIGHT*scale,-UNIT_SIZE*width,

                        //前面1,2
                        UNIT_SIZE*length,UNIT_HIGHT*scale,UNIT_SIZE*width,
                        -UNIT_SIZE*length,UNIT_HIGHT*scale,UNIT_SIZE*width,
                        UNIT_SIZE*length,-UNIT_HIGHT*scale,UNIT_SIZE*width,

                        UNIT_SIZE*length,-UNIT_HIGHT*scale,UNIT_SIZE*width,
                        -UNIT_SIZE*length,UNIT_HIGHT*scale,UNIT_SIZE*width,
                        -UNIT_SIZE*length,-UNIT_HIGHT*scale,UNIT_SIZE*width,

                        //下面11,12
                        -UNIT_SIZE*length,-UNIT_HIGHT*scale,UNIT_SIZE*width,
                        UNIT_SIZE*length,-UNIT_HIGHT*scale,UNIT_SIZE*width,
                        -UNIT_SIZE*length,-UNIT_HIGHT*scale,-UNIT_SIZE*width,

                        -UNIT_SIZE*length,-UNIT_HIGHT*scale,-UNIT_SIZE*width,
                        UNIT_SIZE*length,-UNIT_HIGHT*scale,UNIT_SIZE*width,
                        UNIT_SIZE*length,-UNIT_HIGHT*scale,-UNIT_SIZE*width,

                        //左面9,10
                        -UNIT_SIZE*length,UNIT_HIGHT*scale,-UNIT_SIZE*width,
                        -UNIT_SIZE*length,-UNIT_HIGHT*scale,-UNIT_SIZE*width,
                        -UNIT_SIZE*length,UNIT_HIGHT*scale,UNIT_SIZE*width,

                        -UNIT_SIZE*length,UNIT_HIGHT*scale,UNIT_SIZE*width,
                        -UNIT_SIZE*length,-UNIT_HIGHT*scale,-UNIT_SIZE*width,
                        -UNIT_SIZE*length,-UNIT_HIGHT*scale,UNIT_SIZE*width,

                        //右面3,4
                        UNIT_SIZE*length,UNIT_HIGHT*scale,-UNIT_SIZE*width,
                        UNIT_SIZE*length,UNIT_HIGHT*scale,UNIT_SIZE*width,
                        UNIT_SIZE*length,-UNIT_HIGHT*scale,-UNIT_SIZE*width,

                        UNIT_SIZE*length,-UNIT_HIGHT*scale,-UNIT_SIZE*width,
                        UNIT_SIZE*length,UNIT_HIGHT*scale,UNIT_SIZE*width,
                        UNIT_SIZE*length,-UNIT_HIGHT*scale,UNIT_SIZE*width

                };
        inittempVerteices=new ArrayList<Float>();//存放顶点坐标的ArrayList
        for (int i=0;i<verteices.length;i++){
            inittempVerteices.add(verteices[i]);
        }
        tempVerteices=pointMatrix();
        ByteBuffer vbb=ByteBuffer.allocateDirect(tempVerteices.length*4); //创建顶点坐标数据缓冲
        vbb.order(ByteOrder.nativeOrder());//设置字节顺序
        mVertexBuffer=vbb.asFloatBuffer();//转换为float型缓冲
        mVertexBuffer.put(tempVerteices);//向缓冲区中放入顶点坐标数据
        mVertexBuffer.position(0);//设置缓冲区起始位置
        float[] textureCoors=new float[vCount*2];
        for(int i=0;i<vCount/6;i++)//个顶点纹理坐标
        {
            textureCoors[i*12]=0;
            textureCoors[(i*12)+1]=0;

            textureCoors[(i*12)+2]=0;
            textureCoors[(i*12)+3]=1;

            textureCoors[(i*12)+4]=1;
            textureCoors[(i*12)+5]=0;

            textureCoors[(i*12)+6]=1;
            textureCoors[(i*12)+7]=0;

            textureCoors[(i*12)+8]=0;
            textureCoors[(i*12)+9]=1;

            textureCoors[(i*12)+10]=1;
            textureCoors[(i*12)+11]=1;

        }

        ByteBuffer tbb=ByteBuffer.allocateDirect(textureCoors.length*4);//创建顶点坐标数据缓冲
        tbb.order(ByteOrder.nativeOrder());//设置字节顺序
        mTextureBuffer=tbb.asFloatBuffer();//转换为float型缓冲
        mTextureBuffer.put(textureCoors);//向缓冲区中放入顶点坐标数据
        mTextureBuffer.position(0);//设置缓冲区起始位置
    }

    private void test(){
        //创建绘制顶点数据缓冲
        ByteBuffer vbb = ByteBuffer.allocateDirect(tempVerteices.length*4);
        vbb.order(ByteOrder.nativeOrder());//设置字节顺序
        //mVertexBuffer.clear();
        mVertexBuffer = vbb.asFloatBuffer();//转换为float型缓冲
        mVertexBuffer.put(tempVerteices);//向缓冲区中放入顶点坐标数据
        mVertexBuffer.position(0);//设置缓冲区起始位置
    }
    @Override
    public void drawSelf(GL10 gl) {
        int red=Color.red(color);
        int green=Color.green(color);
        int blue=Color.blue(color);
        int alpha=Color.alpha(color);
        //gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
        gl.glColor4f(red/255.0f,green/255.0f,blue/255.0f, alpha/255.0f);

//        gl.glTranslatef(xOffset, yOffset, zOffset);
//        gl.glRotatef(mAngleY, 0, 1, 0);
//        gl.glRotatef(mAngleZ,0,0,1);
//        if(true)
//        {
//            gl.glScalef(scalezoom, scalezoom, scalezoom);
//        }
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
        if (this.textureId>0){
            gl.glEnable(GL10.GL_TEXTURE_2D);//开启纹理
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);//允许使用纹理数组
            gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);//指定纹理数组
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);//绑定纹理
        }
        gl.glLineWidth(3f);
        gl.glEnable(GL10.GL_POLYGON_OFFSET_FILL);
        gl.glPolygonOffset(5.0f,5.0f);
        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vCount);//绘制
        gl.glColor4f(0,255,0, 1f);//将颜色调为绿色画边框
        gl.glDrawArrays(GL10.GL_LINES, 0, vCount);//绘制边框
        if (this.textureId>0){
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);//关闭纹理数组
            gl.glDisable(GL10.GL_TEXTURE_2D);//关闭纹理
        }

    }
    //点缓存的坐标变换，按矩阵变换来变换
    private float[] pointMatrix(){
        //1.计算坐标变换矩阵
        VaryTools tools=new VaryTools();
        tools.pushMatrix();
        tools.translate(xOffset, yOffset, zOffset);
        tools.rotate(mAngleY, 0, 1, 0);
        tools.rotate(mAngleZ,0,0,1);
        tools.scale(scalezoom, scalezoom, scalezoom);
        float[] mMatrixCurrent=tools.getmMatrixCurrent();
        tools.popMatrix();
        float vertices[]=new float[inittempVerteices.size()];
        for(int i=0;i<inittempVerteices.size();i++)
        {
            vertices[i]=inittempVerteices.get(i);
        }
        //float[] tempVerteices=inittempVerteices;//将点坐标变换为初始状态的坐标重新计算
        for(int i=0;i<vertices.length/3;i++){
            float[] pointMx={vertices[i*3],vertices[i*3+1],vertices[i*3+2],1};
            Matrix.multiplyMV(pointMx,0,mMatrixCurrent,0,pointMx,0);
            vertices[i*3]=pointMx[0];
            vertices[i*3+1]=pointMx[1];
            vertices[i*3+2]=pointMx[2];
        }
        return vertices;
    }
    @Override
    public float[] findMinMax() {
//        tempVerteices=pointMatrix();
        //最大最小值初始化
        minX=tempVerteices[0];//x轴最小位置
        maxX=tempVerteices[0];//x轴最大位置
        minY=tempVerteices[1];//y轴最小位置
        maxY=tempVerteices[1];//y轴最大位置
        minZ=tempVerteices[2];//z轴最小位置
        maxZ=tempVerteices[2];//z轴最大位置
        for(int i=0;i<tempVerteices.length/3;i++)
        {
            //判断X轴的最小和最大位置
            if(tempVerteices[i*3]<minX)
            {
                minX=tempVerteices[i*3];
            }
            if(tempVerteices[i*3]>maxX)
            {
                maxX=tempVerteices[i*3];
            }
            //判断Y轴的最小和最大位置
            if(tempVerteices[i*3+1]<minY)
            {
                minY=tempVerteices[i*3+1];
            }
            if(tempVerteices[i*3+1]>maxY)
            {
                maxY=tempVerteices[i*3+1];
            }
            //判断Z轴的最小和最大位置
            if(tempVerteices[i*3+2]<minZ)
            {
                minZ=tempVerteices[i*3+2];
            }
            if(tempVerteices[i*3+2]>maxZ)
            {
                maxZ=tempVerteices[i*3+2];
            }
        }
        Log.d("Cube minX="+minX,"maxX="+(maxX-minX));
        Log.d("Cube minY="+minY,"maxY="+(maxZ-minZ));
        Log.d("Cube minZ="+minZ,"maxZ="+(maxY-minY));
        float[] length={maxX-minX,maxZ-minZ,maxY-minY};

        return length;
    }

    @Override
    public float[] findMid() {
        midX=(minX+maxX)/2;
        midY=(minY+maxY)/2;
        midZ=(minZ+maxZ)/2;
        Log.d("Cube midX=",""+midX);
        Log.d("Cube midY=",""+midY);
        Log.d("Cube midZ=",""+midZ);
        float[] mid={midX,midY,midZ};
        return mid;
    }

    @Override
    public void setMove(boolean flag) {
        this.moveFlag=flag;
        Log.d("cube",""+this.moveFlag);
    }
    public void setRotate(boolean flag) {//移动
        this.rotateFlag=flag;
        Log.d("cube",""+this.rotateFlag);
    }
    @Override
    public void setHilight(boolean flag) {
        this.hiFlag=flag;
        Log.d("cube",""+this.hiFlag);
    }

    @Override
    public void settexture(int texId) {
        if(texId>0){//如果纹理存在
            this.textureId=texId;
        }
    }

    @Override
    public boolean getMove() {
        return this.moveFlag;
    }

    @Override
    public boolean getRotate() {
        return this.rotateFlag;
    }

    @Override
    public boolean getHilight() {
        return this.hiFlag;
    }

    @Override
    public void Move(float dx, float dy) {
        xOffset=dx;
        yOffset=dy;
        tempVerteices=pointMatrix();
        test();
    }

    @Override
    public void Rotate(float dx, float dy,float ox,float oy) {
        this.mAngleY+=dx*TOUCH_SCALE_FACTOR;//设置延y轴旋转角度
        this.mAngleZ+=dy*TOUCH_SCALE_FACTOR;//设置沿z轴旋转角度
        tempVerteices=pointMatrix();
        test();
        //Log.d(TAG, "Rotate:mAngleY="+mAngleY+" mAngleZ="+mAngleZ);
    }

    @Override
    public void Hilight(float scachange) {
        if(scachange>0){
            this.scalezoom+=0.01f;
        }
        else {
            if(this.scalezoom>=0.2f){
                this.scalezoom-=0.01f;
            }
        }
        tempVerteices=pointMatrix();
        test();
    }

    @Override
    public void setpoint(int pointid, float dx, float dy) {

    }

    @Override
    public void setcolor(int color) {
        this.color=color;
    }

    @Override
    public void meActionup() {

    }
}
